package model;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Observable;

import cs.remoteapp.FileAnswer;
import cs.remoteapp.FileRequest;
import cs.remoteapp.Goodbye;
import cs.remoteapp.Hello;
import cs.remoteapp.TextMessage;
import people.User;
import people.UsersList;

/**
 * Package  model
 * Project ChatSystemG2 - INSA 4INFOA1 - 2013/2014
 * 
 * Model represents the "model" in the MVC design pattern
 * It is an observable, observed by chatNI and chatGUI, and updated by chatController 
 * It contains the informations from chatGUI and chatNI to chatController 
 * It contains the following attributes:
 *
 * @author Sergio Gonzalez - Marie-Charlotte Bideaud
 */

public class ChatModelFacade extends Observable {
	private UsersList userslist;

	/**
	 * Constructor for the class ChatModelFacade
	 * 
	 */
	public ChatModelFacade() {
		super();
		userslist = new UsersList();
	}

	/**
	 * Add a user to the userslist
	 * @param user 
	 */
	public void addUser(User user) {
		userslist.addUser(user);
		setChanged();
		notifyObservers(userslist.getUserslist());
	}

	/**
	 * Remove a user from the userslist
	 * @param user 
	 */
	
	public void removeUser(User user) {
		setChanged();
		notifyObservers(user);
		userslist.removeUser(user);
		setChanged();
		notifyObservers(userslist.getUserslist());
	}
	
	/**
	 * Remove all users from the userslist
	 */
	
	public void removeAllUser() {
		userslist.removeAllUser();
		setChanged();
		notifyObservers(userslist.getUserslist());
	}
	
	/**
	 * Set text message to a user
	 * @param user : User
	 */
	public void setTextMessage (User user) {
		setChanged();
		notifyObservers(user);
	}
	
	/**
	 * Set file request to a user
	 * @param user : User
	 */
	public void setFileRequest(User user) {
		setChanged();
		notifyObservers(user);
	}

	/**
	 * Set file answer to a user
	 * @param user : User
	 */
	public void setFileAnswer(User user, boolean answer) {
		user.setAnswer(answer);
		setChanged();
		notifyObservers(user);
	}
	
	/**
	 * Set file to a user
	 * @param user : User
	 */
	public void setFile(User user) {
		setChanged();
		notifyObservers(user);
	}

	/**
	 * Return a user from its IP address
	 * @param addr 
	 * @return User the user corresponding to addr
	 */
	
	public User getUser(InetAddress addr) {
		return userslist.getUser(addr);
	}
	
	/**
	 * Return the userslist
	 * @return ArrayList<User>
	 */
	public ArrayList<User> getUserList() {
		return userslist.getUserslist();
	}
	
	/**
	 * Create a single hello message
	 * @param username : String
	 * @param sourceAddress : InetAddress
	 */
	public void createHelloReponse(String username, InetAddress sourceAddress){
		setChanged();
		notifyObservers(sourceAddress);
		Hello hello = new Hello(username);
		setChanged();
		notifyObservers(hello);
		
	}
	
	/**
	 * Create a broadcast hello message
	 * @param username : String
	 */
	public void createHelloMessage(String username){
		Hello hello = new Hello(username);
		hello.setBroadcast(true);
		setChanged();
		notifyObservers(hello);
	}
	
	/**
	 * Create a broadcast bye message
	 * @param username : String
	 */
	public void createGoodbyeMessage(String username){
		Goodbye bye = new Goodbye(username);
		setChanged();
		notifyObservers(bye);
	}
	
	/**
	 * Create a 'textmessage' message 
	 * @param user : User
	 */
	public void createTextMessage (User user) {
		setChanged();
		notifyObservers(user.getIPAddr());
		TextMessage textMessage = new TextMessage(user.getUsername(), user.getText());
		setChanged();
		notifyObservers(textMessage);
	}
	
	/**
	 * Create a 'FileRequest' message 
	 * @param user : User
	 */
	public void createFileRequest(User user) {
		setChanged();
		notifyObservers(user.getIPAddr());
		FileRequest fileRequest = new FileRequest(user.getUsername(), user.getFilename(), user.getFilesize());
		setChanged();
		notifyObservers(fileRequest);
	}
	
	/**
	 * Create a 'FileAnswer' message 
	 * @param user : User
	 */
	public void createFileAnswer(User user) {
		setChanged();
		notifyObservers(user.getIPAddr());
		setChanged();
		notifyObservers(user.getPath());
		FileAnswer fileAnswer = new FileAnswer(user.getUsername(), user.getFilename(), user.isAnswer());
		setChanged();
		notifyObservers(fileAnswer);
	}

}
